package com.iw.point.service;

import javax.annotation.Resource;

import com.iw.adoms.entity.Department;
import com.iw.core.orm.PageData;
import com.iw.point.dao.PointRecordDao;
import com.iw.point.entity.PointRecord;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.iw.core.orm.hibernate.BaseDao;
import com.iw.core.orm.hibernate.BaseService;
import com.iw.point.entity.PointMember;

import java.util.List;

@Service
@Transactional
public class PointMemberService extends BaseService<PointMember> {

    @Resource
    private PointRecordDao pointRecordDao;

    @Override
    @Resource(name = "pointMemberDao")
    public void setBaseDao(BaseDao<PointMember> baseDao) {
        this.baseDao = baseDao;
    }

    @Override
    public PageData<PointMember> find(PageData<PointMember> pageData) {
        pageData = super.find(pageData);
        List<PointMember> members = pageData.getResult();
        if (members != null && !members.isEmpty()) {
            for (PointMember m : members) {
                String hql = "select max(r.insertTime),r.id,r.idepart from PointRecord r where r.member.id = ? and 1=?";
                Object[] record = pointRecordDao.find(hql, m.getId(), 1);
                if (record != null && record.length >= 3) {
                    Department dt = (Department) record[2];
                    if (dt != null) {
                        m.setLastDepart(dt.getName());
                    }
                }
            }
        }
        return pageData;    //To change body of overridden methods use File | Settings | File Templates.
    }
}
